﻿using BussinessLogicLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestingSystem.TestingManager
{
    public partial class Preview : Form
    {
        private TEST test = new TEST();

        public Preview(TEST test)
        {
            InitializeComponent();
            this.test = test;
        }

        private void Preview_Load(object sender, EventArgs e)
        {
            var path = Application.StartupPath;
            using (TextReader reader = File.OpenText(path + "//Templates//TestingPreview.html"))
            {
                StringBuilder text = new StringBuilder(reader.ReadToEnd());
                var course = BLL_Course.Get(this.test.COURSE_ID);
                text.Replace("{TEST_DESCRIPTION}", this.test.TEST_DESC);
                text.Replace("{COURSE}", course.COURSE_NAME.Trim());
                text.Replace("{TEST_DURATION}", this.test.TEST_DURATION.ToString());

                StringBuilder textSection = new StringBuilder();
                foreach (var section in test.TEST_SECTIONS)
                {
                    textSection.AppendLine("<p></p>");
                    textSection.AppendLine("<b>" + section.TEST_SECTION_DESC + "(" + section.TEST_SECTION_ITEMS.Count.ToString() + ")</b>" + "<br/>");
                    textSection.AppendLine("<p></p>");

                    var order = 1;
                    var question_ID = -1;
                    var alphabet = 65;

                    if (section.TEST_SECTION_ID != -1)
                    {
                        var questions = BLL_Test_Section.GetTestSectionQuestions(section.TEST_SECTION_ID);
                        
                        foreach (var question in questions)
                        {
                            if (question_ID != question.QUESTION_ID)
                            {
                                textSection.AppendLine(order.ToString() + ". " + question.QUESTION_DESCRIPTION + "</br>");
                                alphabet = 65;
                                order++;
                            }
                            else
                            {
                                textSection.AppendLine("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + Convert.ToChar(alphabet) + ". " + question.ITEM_FACE + "</br>");
                                alphabet++;
                            }
                            question_ID = question.QUESTION_ID;
                        }
                    }
                    else
                    {
                        if (section.TEST_SECTION_ITEMS.Count > 0)
                        {
                            var questionIDs = "";
                            foreach (var item in section.TEST_SECTION_ITEMS)
                            {
                                questionIDs += item.QUESTION_ID + ",";
                            }
                            questionIDs = questionIDs.Trim(new char[] { ',' });

                            if (!string.IsNullOrWhiteSpace(questionIDs))
                            {
                                var questions = BLL_Test_Section.GetTestSectionQuestionIDs(questionIDs);
                                foreach (var question in questions)
                                {
                                    if (question_ID != question.QUESTION_ID)
                                    {
                                        textSection.AppendLine(order.ToString() + ". " + question.QUESTION_DESCRIPTION + "</br>");
                                        alphabet = 65;
                                        order++;
                                    }
                                    else
                                    {
                                        textSection.AppendLine("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + Convert.ToChar(alphabet) + ". " + question.ITEM_FACE + "</br>");
                                        alphabet++;
                                    }
                                    question_ID = question.QUESTION_ID;
                                }
                            }
                        }
                    }
                }
                text.Replace("{SECTIONS}", textSection.ToString());

                this.webPreview.DocumentText = text.ToString();   
            }
        }
    }
}
